import time
import cv2

class ErrorHandler:
    @staticmethod
    def handle_corrupted_video(video_path: str) -> bool:
        try:
            cap = cv2.VideoCapture(video_path)
            if not cap.isOpened():
                print(f"视频文件损坏或无法打开: {video_path}")
                return False
            ret, _ = cap.read()
            cap.release()
            if not ret:
                print(f"无法读取视频帧: {video_path}")
                return False
            return True
        except Exception as e:
            print(f"检查视频文件时出错 {video_path}: {e}")
            return False

    @staticmethod
    def log_error(video_path: str, error: Exception, error_type: str):
        timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
        log_entry = f"[{timestamp}] {error_type}: {video_path} - {error}\n"
        with open("processing_errors.log", "a", encoding="utf-8") as f:
            f.write(log_entry)
